package com.cskaoyan.service;


import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.UserData;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.bean.vo.*;
import com.cskaoyan.module.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/05/21:48
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketRoleMapper roleMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;

    @Override
    public ArrayList<String> getRole(Integer[] roleIds) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(roleIds));
        ArrayList<String> names = new ArrayList<>();
        for (Integer id : roleIds) {
            String name = roleMapper.selectNameById(id);
            names.add(name);
        }
        return (ArrayList<String>) names;
    }

    @Override
    public ArrayList<String> getPerm(Integer[] roleIds) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(roleIds));
        ArrayList<String> permissions = new ArrayList<>();
        for (Integer id : roleIds) {
            List<String> permission = roleMapper.selectPermissions(id);
            for (String s : permission) {
                permissions.add(s);
            }
        }
        return permissions;
    }

    @Override
    public BaseRespVo updatePassword(Map map) {
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();

        Integer id = (Integer) map.get("id");
        String oldPassword = (String) map.get("oldPassword");
        String newPassword = (String) map.get("newPassword");
        String newPassword2 = (String) map.get("newPassword2");
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        String password = marketAdmin.getPassword();
        if (password.equals(oldPassword)&&newPassword.equals(newPassword2)) {
            marketAdmin.setPassword(newPassword);
            marketAdmin.setUpdateTime(new Date());
            adminMapper.updateByPrimaryKey(marketAdmin);
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        }else {
        baseRespVo.setErrno(605);
        baseRespVo.setErrmsg("账号密码不对");}
        return baseRespVo;
    }
}
